<?php
    header('content-type:text/html;charset="utf-8"');
    //定义统一返回格式
    $responseData = array("code" => 0,"message" => "");

    $adminname = $_POST["adminname"];
    $password = $_POST["password"];

    
    //验证
    if(!$adminname){
        $responseData["code"] = 1;
        $responseData["message"] = "用户名不能为空";
        echo json_encode($responseData);
        exit;
    }
    if(!$password){
        $responseData["code"] = 2;
        $responseData["message"] = "密码不能为空";
        echo json_encode($responseData);
        exit;
    }

    // 1.连接数据库
    $link = mysqli_connect("localhost","root","root");

    // 2.判断是否连接成功
    if(!$link){
        $responseData['code'] = 3;
        $responseData['message'] = "数据库连接失败";
        // 返回到前台页面
        echo json_encode($responseData);
        exit; //结束后续操作
    }

    //3.设置字符集
    mysqli_set_charset($link,"utf8");

    //4.选择数据库
    mysqli_select_db($link,"租售系统");

    //5.准备sql 验证用户名、密码是否存在

    // md5加密
    $str = md5(md5(md5($password)."xxx")."yyy");
    
    $sql1 = "SELECT * FROM admin WHERE adminname='{$adminname}' ";
    $sql2 = "SELECT * FROM admin WHERE password='{$str}' ";

    //6.发送sql
    $res1 = mysqli_query($link,$sql1);
    $res2 = mysqli_query($link,$sql2);

    //7.取出一行数据
    $row1 = mysqli_fetch_assoc($res1);
    $row2 = mysqli_fetch_assoc($res2);

    if(!$row1){
        //验证用户是否存在
        $responseData["code"] = 4;
        $responseData["message"] = "用户不存在";
        echo json_encode($responseData);
        exit;
    }else if($row1){
        if(!$row2){
            $responseData["code"] = 5;
            $responseData["message"] = "密码错误";
            echo json_encode($responseData);
            exit;
        }else{
            $responseData["message"] = "登陆成功";
            echo json_encode($responseData);
        }
    }
    
    //8.关闭数据库
    mysqli_close($link);
    
?>